<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Adapter\Controller;
use Think\Controller;
use User\Api\UserApi;
use Think\Model;

class ListController extends Controller {
    public function adapterlist(){
        $list = get_batch();
        $batch = $list['batch'];
        $map['batch'] = $batch;
        $map['status'] = 2;
        $goodlist = M('Purchase')->where($map)->select();
        $this->assign('prolist',$goodlist);
        $this->assign('list',$list);
        $this->title="预购单提交";
        $this->display();
    }
    public function newlist(){
        $batch = date("YmdHis",time());
        $data['batch'] = $batch;
        $data['user'] = session('username');
        $data['createdAt'] = time();
        $data['updatedAt'] = time();
        $data['status'] = 1;
        $map['status'] = array(array('eq',1),array('eq',2), 'or') ;
        $res = M('Purchaselist')->where($map)->select();
        $sqlres=M('Purchaselist')->add($data);
        if($sqlres){
            foreach($res as $key => $value){
                M('Purchase_list')->where($value)->save(array('status'=>0));
            }
            $this->success("新增预购单成功",U('Purchase/index'));
        }else{
            dump($sqlres);
        }
    }
    public function listdetail(){
        $map['batch']=I('batch');
        $list = M('Purchase')->where($map)->select();
        $this->assign("_list",$list);
        $this->batch = I('batch');
        $this->display();
    }
    public function editdetail(){
        $map['batch']=I('batch');
        $this->display();
    }
    public function newproduct(){
        if(IS_POST){
            $data['proName'] = I('proName');
            $data['spec'] = I('spec');
            $data['head'] = I('head');
            $data['jin'] = I('jin');
            $data['unit'] =  I('unit') ;
            $data['number'] =  I('number') ;
            $data['price'] =  I('price');
            $data['netWeight'] = I('netWeight');
            $data['grossWeight'] = I('grossWeight');
            $data['oldPrice'] = I('oldPrice');
            $data['boxfee'] = I('boxfee');
            $data['boxheavy'] = I('boxheavy');
            $data['loss'] = I('loss');
            $data['selflife'] = I('selflife');
            $data['amount'] = I('amount');
            $data['shopName'] = I('shopName');
            $data['shopPhone'] = I('shopPhone');
            $data['locality'] = I('locality');
            $data['arriveTime'] = I('arriveTime');
            $data['remark'] = I('remark');
            $data['status'] = 2;
            $data['purchaser'] = I('purchaser');
            $data['createdAt'] = NOW_TIME ;
            $data['createdUser'] = session('userid') ;
            $data['updatedAt'] = NOW_TIME ;
            $data['updatedUser'] = session('userid') ;
            $data['batch'] = I('batch');
            $res=M('Purchase')->add($data);
            if($res){
                add_log("C","添加商品".$data['proName'],$data['batch']);
                $this->success("成功",U('purchaselist'));
            }else{
                dump($res);
                $this->error("失败");
            }
        }else{
            $batch = I("batch");
            $this->batch = $batch;

            $this->display();
        }
    }
    public function proreport(){
        $map['pid'] = I('pid');
        $map['batch'] = I('batch');
        $map['status'] =1;
        $list = M('Provider_report')->where($map)->select();
        $pname = get_pname($list[0]['pid']);
        $this->pname = $pname;
        $this->title = '商品报价';
        $this->assign('list',$list);
        $this->display();
    }
    public function prodelete(){
        $map['id']=I('id');
        $proName=I('proName');
        $batch=I('batch');
        $re=M('Purchase')->where($map)->delete();

        if($re){
            add_log("D","删除商品".$proName,$batch);
            $this->success('删除成功',U('purchaselist'));
        }else{
            $this->error('删除失败',U('purchaselist'));
        }
    }
    public function profinish (){
        if(IS_POST){
            $map['id'] = I('id');
            $data['proName'] = I('proName');
            $data['spec'] = I('spec');
            $data['head'] = I('head');
            $data['jin'] = I('jin');
            $data['unit'] =  I('unit') ;
            $data['number'] =  I('number') ;
            $data['price'] =  I('price');
            $data['netWeight'] = I('netWeight');
            $data['grossWeight'] = I('grossWeight');
            $data['oldPrice'] = I('oldPrice');
            $data['boxfee'] = I('boxfee');
            $data['boxheavy'] = I('boxheavy');
            $data['loss'] = I('loss');
            $data['selflife'] = I('selflife');
            $data['amount'] = I('amount');
            $data['shopName'] = I('shopName');
            $data['shopPhone'] = I('shopPhone');
            $data['locality'] = I('locality');
            $data['arriveTime'] = I('arriveTime');
            $data['remark'] = I('remark');
            $data['status'] = 2;
            $data['purchaser'] = I('purchaser');
            $data['updatedAt'] = NOW_TIME ;
            $data['updatedUser'] = session('userid');
            $re=M('Purchase')->where($map)->save($data);
            if(I('provider')){
                $report['batch'] = I('batch');
                $report['uid'] = I('provider');
                $report['pid'] = I('pid');
                M('Provider_report')->where($report)->save(array('accept'=>1));
            }
            if($re === false){
                $this->error('确认采购失败',U('purchaselist'));
            }else{
                $this->success('采购已完成',U('purchaselist'));
            }
        }else{
            $map['batch']=I('batch');
            $map['pid']=I('pid');
            if(I('providerid')){
                $this->provider = I('providerid');//如果走的报价流程
                $this->batch = I('batch');//如果走的报价流程
                $this->pid = I('pid');//如果走的报价流程
            }
            $res=M('Purchase')->where($map)->select();
            $this->id = $res[0]['id'];
            $this->assign('pro',$res[0]);
            $this->title = "采购确认";
            $this->display('profinish');
        }

    }
    public function editpro (){
        if(IS_POST){
            $map['id'] = I('id');
            $batch = I('batch');
            $data['proName'] = I('proName');
            $data['spec'] = I('spec');
            $data['head'] = I('head');
            $data['jin'] = I('jin');
            $data['unit'] =  I('unit') ;
            $data['number'] =  I('number') ;
            $data['price'] =  I('price');
            $data['netWeight'] = I('netWeight');
            $data['grossWeight'] = I('grossWeight');
            $data['oldPrice'] = I('oldPrice');
            $data['amount'] = I('amount');
            $data['remark'] = I('remark');
            $data['status'] = 1;
            $data['purchaser'] = I('purchaser');
            $data['updatedAt'] = NOW_TIME ;
            $data['updatedUser'] = session('userid');
            $re=M('Purchase')->where($map)->save($data);
            if($re === false){
                $this->error('修改失败',U('purchaselist'));
            }else{
                add_log("U","编辑商品".$data['proName'],$batch);
                $this->success('修改成功',U('purchaselist'));
            }
        }else{
            $map['id']=I('id');
            $re=M('Purchase')->where($map)->find();
            $this->id = $re['id'];
            $this->batch = $re['batch'];
            $this->assign('pro',$re);
            $this->display();
        }

    }
    public function historylist (){
        $list = M('Purchase_list')->order("createdAt desc")->select();
        $this->assign("_goods",$list);
        $this->title = "历史采购";
        $this->display();
    }
    public function historylist_detail ()
    {
        $map['batch']=I('batch');
        $batch = $map['batch'];
        $goods_map['batch'] = $batch;
        $goods_map['status']= 2;
        $goods_list = M('Purchase')->where($goods_map)->select();
        $this->assign("_goods", $goods_list);
        $this->display();
    }
    public function inventoryybz(){
        $map['BIN ']=array('like',"1%");
        $list = M('vinventory')->where($map)->select();
//        $newpurchase = M('Purchase_list')->where(array('status'=>1))->Order('createdAt desc')->limit(1)->select();
//        $batch = $newpurchase[0]['batch'];
        int_to_string($list);
//        $this->batch = $batch;
        $this->assign('_list', $list);
        $this->title = '原包装库';
        $this->display('List/inventory');
    }
    public function inventorycp(){
        $map['BIN ']=array('like',"2%");
        $list = M('vinventory')->where($map)->select();
//        $newpurchase = M('Purchase_list')->where(array('status'=>1))->Order('createdAt desc')->limit(1)->select();
//        $batch = $newpurchase[0]['batch'];
        int_to_string($list);
//        $this->batch = $batch;
        $this->assign('_list', $list);
        $this->title = '成品库';
        $this->display('List/inventory');
    }
    public function addpurchase(){
        $map['pid'] = I("pid");
        $map['batch'] = I("batch");
        if(count(M("Purchase")->where($map)->select())>0){
            $this->error("该商品已存在，请直接修改");
        }
        $data['proName'] = I('proName');
        $data['spec'] = I('spec');
        $data['head'] = I('head');
        $data['jin'] = I('jin');
        $data['unit'] =  I('unit') ;
        $data['number'] =  I('number') ;
        $data['price'] =  I('price');
        $data['grossWeight'] = I('grossWeight');
        $data['oldPrice'] = I('oldPrice');
        $data['boxfee'] = I('boxfee');
        $data['amount'] = I('amount');
        $data['remark'] = I('remark');
        $data['status'] = I('status');
        $data['purchaser'] = I('purchaser');
        $data['createdAt'] = NOW_TIME ;
        $data['createdUser'] = session('userid') ;
        $data['updatedAt'] = NOW_TIME ;
        $data['updatedUser'] = session('userid') ;
        $data['batch'] = I('batch');
        $data['pid'] = I('pid');
        $res = M('Purchase')->add($data);

        if($res){
            $this->success("添加成功",U('inventory'));

        }else{

            $this->error("添加失败",U('inventory'));

        }
    }
    public function sendlist(){
        $batch = I('batch');
        $map['batch'] = $batch;
        $pidarr =[];
        M('Purchase_list')->where($map)->save(array("status"=>2));
        $goodlist = M('Purchase')->where($map)->select();
        foreach($goodlist as $key => $value){
            array_push($pidarr,$value['pid']);
        }
        $this->success("寻求已发出，等待回复",U('purchaselist'));
    }
    public function adapterhistory(){
        $list = M("Purchase_list")->where(array("status"=>3))->Order('createdAt desc')->select();
    }
    public function reportlist(){
        $batch = I('batch');
        $map['batch'] = $batch;
        $map['status'] = 1;
//        $sql = "SELECT a.batch,pid,uid,price,max(a.updatedAt) updatedAt FROM `gr_provider_report` a where a.batch = '20160218' group by pid,uid";
        $res = M('Purchase')->where($map)->select();
//        $this->assign('list',$list);
        $this->display();
    }
    public function purchaselist(){
        $list = get_batch();
        $batch = $list['batch'];
        $goods_map['batch'] = $batch;
        $goods_map['status'] = array('lt',2);
        $goods_list = M('Purchase')->where($goods_map)->select();
        $map['batch'] = $batch;
        $map['status'] = 1;
        foreach($goods_list as $key=>$val){
            $map['pid'] = $val['pid'];
            if(count(M('Provider_report')->where($map)->select())>0){
                $goods_list[$key]['reportcount'] = 1;
            }
            $old_map['pid'] = $val['pid'];
            $old_map['status'] = array('gt',1);
            $old = M("Purchase")->where($old_map)->order('createdAt desc')->limit(1)->select();
            $oldPrice = "无";
            if($old){
                $oldPrice = $old[0]['price'];
            }
            $goods_list[$key]['oldPrice'] = $oldPrice;
            M("Purchase")->where($map)->save(array("oldPrice"=>$oldPrice));
        }
        $this->assign("_goods",$goods_list);
        $this->list = $list;
        $this->title = "预购单管理";
        $this->display();
    }
    public function purchaselistconfirm(){
        $arriveTime = strtotime(I('arriveTime'));
        $batch = I('batch');
        $map['batch'] = $batch;
        $data['arriveTime'] = $arriveTime;
        $data['status'] = 3;
        $res = M('Purchase_list')->where($map)->save($data);
        if($res){
            $this->success("已移交至财务",U('Purchase/index'));
        }else{
            $this->error("保存失败",U('Purchase/index'));
        }
    }
    public function adapterprodetail(){
        $map['id']=I('id');
        $gooddetail=M('purchase')->where($map)->find();
        $this->assign('pro',$gooddetail);
        $this->display();
    }

}
